package ksj.common.web;

import java.util.Hashtable;

import javax.ws.rs.core.GenericEntity;
import javax.ws.rs.core.Response;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import ksj.common.dto.ResponseDTO;

public class BaseWebService {
	protected ObjectMapper mapper = new ObjectMapper();
	protected static Hashtable<String, String> VERIFICATION_CODE_TABLE = new Hashtable<>();

	protected Response createResponse(int status, String message, Object response) {
		return createResponse(status, message, response, -1);
	}

	@JsonIgnore
	protected Response createResponse(int status, String message, Object response, int resultSetCount) {
		GenericEntity<String> payloadEntity;
		try {
			payloadEntity = new GenericEntity<String>(
					mapper.writeValueAsString(new ResponseDTO(status, message, response, resultSetCount))) {
			};
//			return Response.ok(payloadEntity).header("Access-Control-Allow-Origin", "*")
//					.header("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT").build();
			return Response.ok(payloadEntity).build();
		} catch (JsonProcessingException e) {
			e.printStackTrace();
			return Response.ok(" {\"status\": 501,\n\"message\": \"server_error\",\n\"response\": \"\"}")
					.header("Access-Control-Allow-Origin", "*")
					.header("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT").build();
		} catch (Exception e) {
			e.printStackTrace();
			return Response.ok(" {\"status\": 501,\n\"message\": \"server_error\",\n\"response\": \"\"}")
					.header("Access-Control-Allow-Origin", "*")
					.header("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT").build();
		}
		// return
		// Response.ok(" {\"status\": 501,\n\"message\":
		// \"server_error\",\n\"response\": \"\"}").build();
		// return
		// Response.ok(" {\"status\": 200,\n\"message\":
		// \"done!\",\n\"response\": \"\"}").build();

	}

}
